<template>
  <div class="reg">
    <div class="box">
      <img src="@/assets/images/login_title.png" alt="" />
      <el-form :rules="rules" :model="form" style="width:360px" ref="aa">
        <el-form-item class="inp" prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model="form.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item class="inp" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="inp" prop="repassword">
          <el-input
            placeholder="请再次输入密码"
            v-model="form.repassword"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-button class="inp" :loading="loading" type="primary" @click="submit"
          >注册</el-button
        >
      </el-form>
      <span>
        <router-link to="/login">去登录</router-link>
      </span>
    </div>
  </div>
</template>

<script>
// import { getCC } from '@/api/index'
import { apiReg } from '@/api/login'
export default {
  data () {
    return {
      loading: false,
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10的字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到10个字符', tigger: 'change' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到10个字符', tigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (value === this.form.password) {
                callback()
              } else {
                callback(new Error('请保持密码一致'))
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.aa.validate(result => {
        if (result === true) {
          this.loading = true
          apiReg(this.form)
            .then(res => {
              // this.loading = false
              // if (res.data.code === 0) {
              this.$message.success('注册成功')
              this.$router.push('/login')
              // } else if (res.data.code === 1) {
              //   this.$message.error(res.data.message)
              // }
            })
            .catch(err => {
              console.log(err)
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg {
  background: url('~@/assets/images/login_bg.jpg') center no-repeat;
  height: 100%;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 360px;
    background-color: #fff;
    text-align: center;
    padding: 20px 25px;
    .inp {
      width: 360px;
      margin-top: 23px;
    }
    span {
      display: block;
      margin-top: 10px;
      a {
        color: #999;
        font-size: 12px;
        position: relative;
        left: -160px;
      }
    }
  }
}
</style>
